package Model;
import java.util.*;



public class SetSubTaskStatusCommand implements Command
{

private ComplexTask ParentTask;
private Task ChildChangedTask;
private ArrayList<Task> TasksPointer;
private ArrayList<Task> SubTasksPointer;
private boolean LastStatus;
private boolean CurrentStatus;
private boolean undone;


//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public SetSubTaskStatusCommand(ArrayList<Task> PointerToTasks)
	{
		TasksPointer=PointerToTasks;
        undone=false;
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void execute(String taskName, boolean isTaskResolved)
	{
        for(int i = 0; i < TasksPointer.size(); ++i)
		{
            if(TasksPointer.get(i).getType().equals("Complex"))
            {
               ArrayList<Task> temp = TasksPointer.get(i).getSubtasks();


               for(int j = 0; j<temp.size() ; ++j )
               {
                  if(temp.get(j).getName().equals(taskName))
                  {
                    ParentTask = (ComplexTask)TasksPointer.get(i);
                    SubTasksPointer = temp;
                    ChildChangedTask = temp.get(j);
                    LastStatus = ChildChangedTask.getDone();
                    CurrentStatus = isTaskResolved;
                    ChildChangedTask.setDone(CurrentStatus);
                    break;

                  }
               }//end of inner foor loop
            }
		}//end of outer for loop
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void undo()
	{
        if(ChildChangedTask != null && undone==false)
        {
            SetToLastStatus();
            undone=true;
        }
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

	public void redo()
	{
        if(ChildChangedTask !=null && undone)
        {
            SetToLastStatus();
            undone=false;
        }
	}

	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------

    private void SetToLastStatus()
    {
        if(ChildChangedTask != null)
        {
            ChildChangedTask.setDone(LastStatus);
            boolean temp;
            temp = LastStatus;
            LastStatus=CurrentStatus;
            CurrentStatus=temp;
        }

    }

    //-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
	//-----------------------------------------------------------------------------------
}



